package com.esperia09.webservice.mixiVoiceNotify.common;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;

public class SendRequest {
	private static final int CONNECTION_TIMEOUT = 20000;

	/**
	 * post
	 * @param postString
	 * @return
	 * @throws IOException
	 */
	public static String sendPost(HttpURLConnection con, String postString) throws IOException {
		String result = "";
		BufferedReader in = null;
		PrintWriter writer = null;

	    con.setRequestMethod("POST");
	    con.setDoOutput(true);
	    con.setConnectTimeout(CONNECTION_TIMEOUT);
		
		try {
		    writer = new PrintWriter(con.getOutputStream());
		    writer.print(postString);
		    writer.close();

		    in = new BufferedReader(new InputStreamReader(con.getInputStream()));

		    String line;
		    while ((line = in.readLine()) != null) {
		        result += line;
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
		    if (in != null) in.close();
		}
		
		return result;
	}

	/**
	 * post
	 * @param postString
	 * @return
	 * @throws IOException
	 */
	public static String sendGet(HttpURLConnection con) throws IOException {
		String result = "";
		BufferedReader in = null;

	    con.setRequestMethod("GET");
	    con.setDoOutput(true);
	    con.setConnectTimeout(CONNECTION_TIMEOUT);
		
		try {
		    in = new BufferedReader(new InputStreamReader(con.getInputStream()));

		    String line;
		    while ((line = in.readLine()) != null) {
		        result += line;
		    }
		} catch (Exception e) {
		    e.printStackTrace();
		} finally {
		    if (in != null) in.close();
		}
		
		return result;
	}
}
